package com.easyadmin.modules.sys.controller;

import com.easyadmin.common.context.UserContext;
import com.easyadmin.common.tools.SequenceUtil;
import com.github.pagehelper.PageInfo;
import com.easyadmin.modules.sys.domain.BCustomerInfo;
import com.easyadmin.common.web.base.BaseController;
import com.easyadmin.common.web.domain.request.PageDomain;
import com.easyadmin.common.web.domain.response.Result;
import com.easyadmin.common.web.domain.response.module.ResultTable;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.easyadmin.modules.sys.service.IBCustomerInfoService;

import java.util.Arrays;

/**
 * 客户信息Controller
 *
 * @author mars
 * @date 2025-02-14
 */
@Api(tags = {"客户管理"})
@RestController
@RequestMapping("/sys/customerInfo")
public class BCustomerInfoController extends BaseController {

    private String prefix = "sys/customerInfo";

    @Autowired
    private IBCustomerInfoService bCustomerInfoService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/sys/customerInfo/main','sys:customerInfo:main')")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询客户信息列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/sys/customerInfo/data','sys:customerInfo:data')")
    public ResultTable list(@ModelAttribute BCustomerInfo bCustomerInfo, PageDomain pageDomain) {
        PageInfo<BCustomerInfo> pageInfo = bCustomerInfoService.selectBCustomerInfoPage(bCustomerInfo, pageDomain);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 新增客户信息
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/sys/customerInfo/add','sys:customerInfo:add')")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增客户信息
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/sys/customerInfo/add','sys:customerInfo:add')")
    public Result save(@RequestBody BCustomerInfo bCustomerInfo) {
        // 获取当前用户id
        String userId = UserContext.currentUser().getUserId();
        bCustomerInfo.setUserId(userId);
        return decide(bCustomerInfoService.save(bCustomerInfo));
    }

    /**
     * 修改客户信息
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/sys/customerInfo/edit','sys:customerInfo:edit')")
    public ModelAndView edit(Long id, ModelMap map) {
        BCustomerInfo bCustomerInfo = bCustomerInfoService.getById(id);
        map.put("bCustomerInfo", bCustomerInfo);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改客户信息
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/sys/customerInfo/edit','sys:customerInfo:edit')")
    public Result update(@RequestBody BCustomerInfo bCustomerInfo) {
        return decide(bCustomerInfoService.updateById(bCustomerInfo));
    }

    /**
     * 删除客户信息
     */
    @ResponseBody
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/sys/customerInfo/remove','sys:customerInfo:remove')")
    public Result batchRemove(String ids) {
        return decide(bCustomerInfoService.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 删除客户信息
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/sys/customerInfo/remove','sys:customerInfo:remove')")
    public Result remove(@PathVariable("id") Integer id) {
        return decide(bCustomerInfoService.removeById(id));
    }
}
